<?php

namespace app\Index\controller;

use think\Controller;
use think\Request;
use think\Facade\Config;
use Units\Http;
class Base extends Controller
{
    public function __construct()
    {
        parent::__construct();
        if(!session('username'))
        {
            $this->redirect(url('/login'));
        }
    }

    protected function guid(){
        if (function_exists('com_create_guid')){
            return str_replace(['{','}'],'',com_create_guid());
        }else{
            mt_srand((double)microtime()*10000);//optional for php 4.2.0 and up.
            $charid = strtoupper(md5(uniqid(rand(), true)));
            $hyphen = chr(45);// "-"
            $uuid = chr(123)// "{"
                .substr($charid, 0, 8).$hyphen
                .substr($charid, 8, 4).$hyphen
                .substr($charid,12, 4).$hyphen
                .substr($charid,16, 4).$hyphen
                .substr($charid,20,12)
                .chr(125);// "}"
            return $uuid;
        }
    }
    protected function getLatlng($url){
        $url = str_replace(' ','+' ,$url);
        $url = "https://maps.googleapis.com/maps/api/geocode/json?address=".$url."&key=".Config::get('GOOGLE_MAP_API_KEY');
        $res = Http::instance()->request( $url , 'post')->data();
        $res = json_decode($res , true);
        if( $res['status']=='OK') {
            return $res['results']['0']['geometry']['location'];
        }else{
            return ['lat'=>'0','lng'=>'0'];
        }
    }
}
